package parkshop.pmcourse.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.Advertisement;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.mapping.AdvertisementMapper;
import parkshop.pmcourse.mapping.ShopMapper;
import parkshop.pmcourse.service.AdvertisementService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class AdvertisementServiceImpl implements AdvertisementService{

	@Autowired
	private AdvertisementMapper advertisementMapper;
	
	@Autowired
	private ShopMapper shopMapper;
	
	@Override
	public boolean saveAdvertisement(Advertisement advertisement, User user,HttpServletRequest request) {
		// TODO Auto-generated method stub
		Long shopId=(long) ConstantUtil.adminShopMark;//  管理员的商店
		if(ConstantUtil.userTypeShopOwner.equals(user.getType())){
			Shop shop=shopMapper.selectByShopOwnerId(user.getId());
			shopId=shop.getId();
		}
		advertisement.setShopId(shopId);		
		List<Advertisement> list=advertisementMapper.selectByShopId(shopId);
		
		if(list==null||list.size()>ConstantUtil.maxAdvertisementNumber)// 允许的最大广告数量. 广告不能过多。   管理广告也不用分页
			return false;
		
		String localHost="http://"+request.getServerName()+":"+request.getServerPort();
		if(advertisement.getUrl().startsWith(localHost)){
			advertisement.setUrl(advertisement.getUrl().substring(localHost.length()));
		}
		
		int rows=advertisementMapper.insertSelective(advertisement);
		return rows==1?true:false;
	}

	@Override
	public List<Advertisement> getAdvertisementsByUserId(String userType,Long userId) {
		// TODO Auto-generated method stub
		
		Long id=(long) ConstantUtil.adminShopMark;
		
		if(!ConstantUtil.userTypeAdministrator.equals(userType)){
			id=shopMapper.selectByShopOwnerId(userId).getId();
		}
		List<Advertisement> list=advertisementMapper.selectByShopId(id);
		return list;
	}
	@Override
	public List<Advertisement> getAdvertisementsByShopId(Long shopId) {
		// TODO Auto-generated method stub
		List<Advertisement> list=advertisementMapper.selectByShopId(shopId);
		return list;
	}

	@Override
	public boolean deleteAdvertisements(Long advertisementId, User user) {
		// TODO Auto-generated method stub
		// 每个卖家只能删除自己的广告
		Long id=(long) ConstantUtil.adminShopMark;
		if(!ConstantUtil.userTypeAdministrator.equals(user.getType())){
			Shop shop=shopMapper.selectByShopOwnerId(user.getId());
			id=shop.getId();
		}
			
		if(id!=advertisementMapper.selectByPrimaryKey(advertisementId).getShopId())
			return false;
		int rows=advertisementMapper.deleteByPrimaryKey(advertisementId);		
		return rows==1?true:false;
	}

	@Override
	public boolean updateAdvertisements(Advertisement advertisement,HttpServletRequest request) {
		// TODO Auto-generated method stub
		String localHost="http://"+request.getServerName()+":"+request.getServerPort();
		if(advertisement.getUrl().startsWith(localHost)){
			advertisement.setUrl(advertisement.getUrl().substring(localHost.length()));
		}
		int rows=advertisementMapper.updateByPrimaryKeySelective(advertisement);
		return rows==1?true:false;
	}

	@Override
	public Advertisement getAdvertisementByADId(Long id) {
		// TODO Auto-generated method stub
		return advertisementMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageBean getPageAdvertisementsByUserId(String userType, Long userId, int pageNumber) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean();
		
		Long id=(long) ConstantUtil.adminShopMark;		
		if(!ConstantUtil.userTypeAdministrator.equals(userType)){
			id=shopMapper.selectByShopOwnerId(userId).getId();
		}
		pageBean.setId(id);
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.pageSize);
		pageBean.setPageSize(ConstantUtil.pageSize);
		
		List<Advertisement> list=advertisementMapper.selectPageByShopId(pageBean);
		
		pageBean.setRecordList(list);
		pageBean.setCurrentPage(pageNumber);
		int count=advertisementMapper.selectCountByShopId(id);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		pageBean.setRecordCount(count);
		return pageBean;
	}

}
